package memorphic.samplr.views
{
	import flash.display.DisplayObject;
	
	import memorphic.samplr.ISamplrAppProps;
	import memorphic.samplr.controllers.GalleryController;
	import memorphic.samplr.models.Gallery;
	import memorphic.samplr.views.popups.NewGalleryPopup;
	import memorphic.slide.IStateView;
	import memorphic.slide.SlideDelegate;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;

	public class NavigationPresModel extends SlideDelegate
	{
		
		[Bindable]
		public var appProps:ISamplrAppProps;
		
		[Bindable]
		public var title:String = "My Galleries";
		
		
		[Bindable]
		[Trigger(path="appProps.galleries")]
		public var galleries:ArrayCollection;
		
		
		public const galleryTitleField:String = "title";
		
		
		public function NavigationPresModel(view:ISamplrAppProps)
		{
			super(view as IStateView);
			appProps = view;
		}
		
		
		public function showGallery(gallery:Gallery):void
		{
			if (gallery == GalleryController.NEW_GALLERY)
			{
				var newGalPopup:NewGalleryPopup = new NewGalleryPopup();
				newGalPopup.addEventListener(CloseEvent.CLOSE, onPopupClose);
				PopUpManager.addPopUp(newGalPopup,NavigationColumn(appProps).parentApplication as DisplayObject,true);
				PopUpManager.centerPopUp(newGalPopup);
			}
			else
			{
				appProps.galleryController.showGallery(gallery);
			}
		}
		
		private function onPopupClose(evt:CloseEvent):void
		{
			var ngp:NewGalleryPopup = NewGalleryPopup(evt.target);
			ngp.removeEventListener(CloseEvent.CLOSE, onPopupClose);
			PopUpManager.removePopUp(ngp);
			
			if (evt.detail == Alert.OK)
			{
				var gal:Gallery = new Gallery(ngp.newGalleryTitle);
				appProps.galleryController.addGallery(gal);
			}
		}
	}
}